﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using TaskManagement.ModelLayer;
using TaskManagement.BusinessLayer;

namespace TaskManagement.WcfServiceLibrary
{
    public class TaskService : ITaskService
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        #region Task
        public IList<Task> SearchTasks(Task t)
        {
            TaskControler tc = new TaskControler();
            return tc.SearchTask(t);
        }

        public IList<Task> SearchTasksRest(int id, string description, string account, string delivery, string tag, DateTime date, string status, int estimate, int timespent, int employeeid)
        {
            Task t = CreateTaskFromData(date, description, account, delivery, tag, status, estimate, timespent, id, employeeid);
            TaskControler tc = new TaskControler();
            return tc.SearchTask(t); ;
        }

        public int CreateTask(Task task)
        {
            TaskControler tc = new TaskControler();
            return tc.CreateTask(task);
        }

        public int CreateTaskRest(string description, string account, string delivery, string tag, DateTime date, string status, int estimate, int timespent)
        {
            Task t = CreateTaskFromData(date, description, account, delivery, tag, status, estimate, timespent);
            TaskControler tc = new TaskControler();
            return tc.CreateTask(t);
        }

        public int DeleteTasks(IList<Task> tasks)
        {
            TaskControler tc = new TaskControler();
            return tc.DeleteTasks(tasks);
        }

        public int DeleteTaskRest(int id)
        {
            TaskControler tc = new TaskControler();
            Task t = new Task();
            t.Id = id;
            List<Task> tasks = new List<Task>(1);
            tasks.Add(t);
            return tc.DeleteTasks(tasks);
        }

        private Task CreateTaskFromData(DateTime date, string description = null, string account = null, string delivery = null, string tag = null, string status = null, int estimate = 0, int timespent = 0,int id = 0, int employeeid = 0 )
        {
            Task t = new Task();
            if (id != 0)
                t.Id = id;

            if (description != null)
                t.Description = description;

            if (account != null)
                t.Account = account;

            if (delivery != null)
                t.Delivery = delivery;

            if (tag != null)
                t.Tag = tag;

            if (date != DateTime.MinValue)
                t.Date = date;

            if (status != null)
                t.Status = status;

            if (estimate != 0)
                t.Estiamte = estimate - 1;

            if (timespent != 0)
                t.TimeSpent = timespent - 1;

            if (employeeid != 0)
            {
                Employee e = new Employee();
                e.Id = employeeid - 1;
                t.Employee = e;
            }

            return t;
        }

        public int UpdateTask(Task task)
        {
            TaskControler tc = new TaskControler();
            return tc.UpdateTask(task);
        }

        public int UpdateTaskRest(int id, string description, string account, string delivery, string tag, DateTime date, string status, int estimate, int timespent)
        {
            TaskControler tc = new TaskControler();
            Task task = CreateTaskFromData(date, description, account, delivery, tag, status, estimate, timespent, id);
            return tc.UpdateTask(task);
        }

        public IList<string> GetAccounts()
        {
            TaskControler tc = new TaskControler();
            return tc.GetAccounts();
        }

        public IList<string> GetTags()
        {
            TaskControler tc = new TaskControler();
            return tc.GetTags();
        }

        public IList<string> GetDeliverys()
        {
            TaskControler tc = new TaskControler();
            return tc.GetDeliverys();
        }

        #endregion

        #region Workflow

        public List<Workflow> SearchWorkflow(Workflow searchWorkflow)
        {
            WorkflowControler wc = new WorkflowControler();
            return wc.SearchWorkflows(searchWorkflow);
        }

        public List<Workflow> SearchWorkflowRest(int id, string name, string description, DateTime startdate)
        {
            WorkflowControler wc = new WorkflowControler();
            Workflow w = CreateWorkflowFromDate(startdate, id, name, description);
            return wc.SearchWorkflows(w);
        }

        private Workflow CreateWorkflowFromDate(DateTime date,int wId = 0,string wName = null,string wDescription = null)
        {
            Workflow w = new Workflow();
            
            if (wId != 0)
                w.Id = wId;

            if (date != DateTime.MinValue)
                w.StartDate = date;

            if (!string.IsNullOrWhiteSpace(wName))
                w.Name = wName;

            if (!string.IsNullOrWhiteSpace(wDescription))
                w.Description = wDescription;

            return w;
        }

        public int CreateWorkflow(Workflow w)
        {
            WorkflowControler wc = new WorkflowControler();
            return wc.CreateWorkflow(w);
        }

        public int CreateWorkflowRest(string name, string description, DateTime startdate)
        {
            Workflow w = CreateWorkflowFromDate(startdate,wName:name,wDescription:description);
            WorkflowControler wc = new WorkflowControler();
            return wc.CreateWorkflow(w);
        }

        public string UpdateWorkflow(Workflow workflow)
        {
            WorkflowControler wc = new WorkflowControler();
            return wc.UpdateWorkflow(workflow);
        }

        public string UpdateWorkflowRest(int id, string name, string description, DateTime startdate)
        {
            Workflow w = CreateWorkflowFromDate(startdate, id, name, description);
            WorkflowControler wc = new WorkflowControler();
            return wc.UpdateWorkflow(w);
        }

        public int DeleteWorkflows(IList<Workflow> workflows)
        {
            WorkflowControler wc = new WorkflowControler();
            return wc.DeleteWorkflows(workflows);
        }

        public int DeleteWorkflow(int id)
        {
            WorkflowControler wc = new WorkflowControler();
            Workflow w = new Workflow();
            w.Id = id;
            w.StartDate = DateTime.MinValue;
            List<Workflow> list = new List<Workflow>();
            list.Add(w);
            return wc.DeleteWorkflows(list);
        }

        #endregion



       
    }
}
